package plugin;

import Main.Parameters;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import system.MessageOUTPUT;

/**
 *
 * @author zem
 */
public class PluginLoader {
    public static boolean Error;
    static PluginInterface instance;
    static String plugin_name;
 /**
     * Getting names of parameters
     * @return String[] of parameters names
     */
    public String[] GetPluginParameters() {
     try {

            return instance.ParameterNames();

        } catch (Exception error) {

             String[] mes = {plugin_name};
            new MessageOUTPUT().OUTPUT("er0017",mes);
            return null;

        }
    }

    /**
     * Getting group names
     * @return String[] of group names
     */
     public String[] GetPluginGroups() {
     try {

            return instance.GroupNames();

        } catch (Exception error) {

             String[] mes = {plugin_name};
            new MessageOUTPUT().OUTPUT("er0017",mes);
            return null;

        }
    }

     /**
      * Getting parameters in each group
      * @return String[][] of parameters in each group
      */
     public String[][] GetPluginGroupParameters() {
     try {

            return instance.GroupParameterNames();

        } catch (Exception error) {

             String[] mes = {plugin_name};
            new MessageOUTPUT().OUTPUT("er0017",mes);
            return null;

        }
    }
    /**
     * Load plugin
     * @param name
     * @return true if loaded successfully
     */
    public boolean Load(String name) {
        Error=false;
        plugin_name=name;

        File jar = new File(Parameters.PluginDirectory + File.separatorChar + name+File.separatorChar+name+".jar");


        // Checking plugin
       
            try {
                URL jarURL = jar.toURI().toURL();

           if(true) {return true;} else return false;
            } catch (Exception e) {
                System.out.println(e);
                  return false;
            }

        
    }

    /**
     * Start plugin
     * @return true if start successfully
     */
    public boolean Start() {
    try {
            Error=false;
             String[] mes = {plugin_name};
             new MessageOUTPUT().OUTPUT("ev0011",mes);
            instance.Start();
            return true;
        } catch (Exception er) {

            Error=true;
              String[] mes = {plugin_name};
             new MessageOUTPUT().OUTPUT("er0019",mes);
             return false;
        }
    }

    /**
     * Stop plugin
     * @return true if success
     */
    public boolean Stop() {
        try {
            Error=false;
            instance.Stop();
            return true;
        } catch (Exception er) {
            Error=true;
              String[] mes = {plugin_name};
            new MessageOUTPUT().OUTPUT("er0013",mes);
            return false;
        }
    }

    /**
     * Check if plugin has been crashes
     * @return true if crashed
     */
    public boolean Crashed() {
        try {
            Error=false;
            return instance.Crashed();
        } catch (Exception error) {
            Error=true;
              String[] mes = {plugin_name};
            new MessageOUTPUT().OUTPUT("er0014",mes);
            return false;
        }

    }
    /**
     * Get crash error
     * @return crash error
     */
    public String CrashError() {
        try {
            Error=false;
            return instance.CrashError();
        } catch (Exception error) {
            Error=true;
             String[] mes = {plugin_name};
            new MessageOUTPUT().OUTPUT("er0016",mes);
            return null;
        }

    }
    
    
    /**
     * Check if plugin need start parameters
     * @return true if needed
     */
    public boolean HasParameters() {
    try {
            Error=false;
            return instance.hasParameters();
        } catch (Exception error) {
            Error=true;
             String[] mes = {plugin_name};
            new MessageOUTPUT().OUTPUT("er0018",mes);
            return false;
        }
    }

    /**
     * Getting names of parameters
     * @return true if successfully added
     */
    public boolean GetParameters() {
     try {
            Error=false;
            return true;
        } catch (Exception error) {
            Error=true;
             String[] mes = {plugin_name};
            new MessageOUTPUT().OUTPUT("er0017",mes);
            return false;

        }
    }

    /**
     * Getting information about plugin
     * @return String about plugin
     */
    public String About() {
        return instance.About();
    }

    /**
     * Input plugin parameters
     * @param Plugin_String_parameter Plugin parameters
     * @param Plugin_Group_String_parameter Plugin group parameters
     * @return true if successfully input
     */
    public boolean InputParameters(String[][] Plugin_String_parameter,String[][][][] Plugin_Group_String_parameter) {
        try{
        return true;
        }catch(Exception er){return false;}
    }
}


